/* This do file provides the code to generate A2.7 in Appendix 2. The 
procedure is described in detail in Appendix 7. */


global dataDir "D:\swweschl\Dropbox\Party_Interactions_Perceptions\PSRM Submission\Replication Files"
use "$dataDir/replication_data.dta", clear


set more off

/*create a dyad-survey ID */
capture drop dyad
egen dyad=group(groupdyad year)

/* placeholder to store the 500 coefficients of various variables in different models */
foreach model in A B C D E F{


capture drop mediascore_`model'
gen mediascore_`model'=.

capture drop mediascoreSE_`model'
gen mediascoreSE_`model'=.

capture drop ideological_distance_`model'
gen ideological_distance_`model'=.

capture drop ideological_distanceSE_`model'
gen ideological_distanceSE_`model'=.

capture drop cab_membership_`model'
gen cab_membership_`model'=.

capture drop cab_membershipSE_`model'
gen cab_membershipSE_`model'=.

capture drop constant_`model'
gen constant_`model'=.

capture drop constantSE_`model'
gen constantSE_`model'=.

capture drop dyadRE_`model'
gen dyadRE_`model'=.

capture drop dyadRE_SE_`model'
gen dyadRE_SE_`model'=.


capture drop respRE_`model'
gen respRE_`model'=.

capture drop respRE_SE_`model'
gen respRE_SE_`model'=.

capture drop residRE_`model'
gen residRE_`model'=.

capture drop residRE_SE_`model'
gen residRE_SE_`model'=.


}

capture drop partyA_temp
capture drop partyB_temp

egen partyA_temp=group(dyad_name1 country)
egen partyB_temp=group(dyad_name2 country)

/* Model A */ 

set more off 
local row=1
foreach row of numlist 1/500{


tempvar order
generate long `order' = _n


capture drop onedraw_partyA_rile
gen onedraw_partyA_rile=rnormal(rile_partyA1,rile_partyA_sd1) /* draw one rile score for party A from the distribution */
bysort partyA_temp: replace onedraw_partyA_rile = onedraw_partyA_rile[1] /* make sure that the draw is not for 1 observation, but constant within dyad */



capture drop onedraw_partyB_rile
gen onedraw_partyB_rile=rnormal(rile_partyB1,rile_partyB_sd1) /* draw one rile score for party B from the distribution */
bysort partyB_temp: replace onedraw_partyB_rile = onedraw_partyB_rile[1] /* make sure that the draw is not for 1 observation, but constant within dyad */
sort `order'


capture drop diff_rile /* calculate the diff in rile among the pair */
gen diff_rile=abs(onedraw_partyA_rile-onedraw_partyB_rile)


capture drop onedraw_coopscore_full
gen onedraw_coopscore_full=rnormal(coopscore_meanfull,coopscore_sdfull) /* draw one media score from the distribution */
bysort dyad: replace onedraw_coopscore_full = onedraw_coopscore_full[1] /* make sure that the draw is not for 1 observation, but constant within dyad */


mixed coop onedraw_coopscore_full /// /* use that one draw of the media score in this regression */
				 diff_rile ///
				 current_bothCab i.countryyear ///
, ||_all:R.groupdyad || respid:	 

				  

				  
replace mediascore_A=_b[onedraw_coopscore_full] in `row' 
replace mediascoreSE_A=_se[onedraw_coopscore_full] in `row' 				  

replace ideological_distance_A=_b[diff_rile] in `row' /* store the coefficient of rile in this variable */
replace ideological_distanceSE_A=_se[diff_rile] in `row' /* store the se of rile in this variable */

replace cab_membership_A=_b[current_bothCab] in `row' /* store the coefficient of cab participation in this variable */
replace cab_membershipSE_A=_se[current_bothCab] in `row' /* store the se of cab participation in this variable */

replace constant_A=_b[_cons] in `row' 
replace constantSE_A=_se[_cons] in `row' 

replace dyadRE_A=(exp([lns1_1_1]_b[_cons]))^2 in `row'
 _diparm lns1_1_1, f(exp(@)^2) d(2*exp(@)^2)
replace dyadRE_SE_A=r(se) in `row'

replace respRE_A=(exp([lns2_1_1]_b[_cons]))^2 in `row'
 _diparm lns2_1_1, f(exp(@)^2) d(2*exp(@)^2)
replace respRE_SE_A=r(se) in `row'

replace residRE_A=(exp([lnsig_e]_b[_cons]))^2 in `row'
 _diparm lnsig_e, f(exp(@)^2) d(2*exp(@)^2)
replace residRE_SE_A=r(se) in `row'

local row=`row'+1

}



/* Model B */ 

set more off 
local row=1
foreach row of numlist 1/500{

tempvar order
generate long `order' = _n


capture drop onedraw_partyA_rile
gen onedraw_partyA_rile=rnormal(rile_partyA1,rile_partyA_sd1) /* draw one rile score for party A from the distribution */
bysort rile_partyA1: replace onedraw_partyA_rile = onedraw_partyA_rile[1] /* make sure that the draw is not for 1 observation, but constant within dyad */

capture drop onedraw_partyB_rile
gen onedraw_partyB_rile=rnormal(rile_partyB1,rile_partyB_sd1) /* draw one rile score for party B from the distribution */
bysort rile_partyB1: replace onedraw_partyB_rile = onedraw_partyB_rile[1] /* make sure that the draw is not for 1 observation, but constant within dyad */
sort `order'

capture drop diff_rile /* calculate the diff in rile among the pair */
gen diff_rile=abs(onedraw_partyA_rile-onedraw_partyB_rile)


capture drop onedraw_coopscore_full
gen onedraw_coopscore_full=rnormal(coopscore_meanfull,coopscore_sdfull) /* draw one media score from the distribution */
bysort dyad: replace onedraw_coopscore_full = onedraw_coopscore_full[1] /* make sure that the draw is not for 1 observation, but constant within dyad */




mixed coop onedraw_coopscore_full /// /* use that one draw of the media score in this regression */
				 diff_rile ///
				 perceived_bothCab i.countryyear, ||_all:R.groupdyad || respid:	
				  

				  
replace mediascore_B=_b[onedraw_coopscore_full] in `row' 
replace mediascoreSE_B=_se[onedraw_coopscore_full] in `row' 				  
				  

replace ideological_distance_B=_b[diff_rile] in `row' /* store the coefficient of rile in this variable */
replace ideological_distanceSE_B=_se[diff_rile] in `row' /* store the se of rile in this variable */

replace cab_membership_B=_b[perceived_bothCab] in `row' /* store the coefficient of cab participation in this variable */
replace cab_membershipSE_B=_se[perceived_bothCab] in `row' /* store the se of cab participation in this variable */


replace constant_B=_b[_cons] in `row' 
replace constantSE_B=_se[_cons] in `row' 

replace dyadRE_B=(exp([lns1_1_1]_b[_cons]))^2 in `row'
 _diparm lns1_1_1, f(exp(@)^2) d(2*exp(@)^2)
replace dyadRE_SE_B=r(se) in `row'

replace respRE_B=(exp([lns2_1_1]_b[_cons]))^2 in `row'
 _diparm lns2_1_1, f(exp(@)^2) d(2*exp(@)^2)
replace respRE_SE_B=r(se) in `row'

replace residRE_B=(exp([lnsig_e]_b[_cons]))^2 in `row'
 _diparm lnsig_e, f(exp(@)^2) d(2*exp(@)^2)
replace residRE_SE_B=r(se) in `row'


local row=`row'+1

}


/* Model C */ 

set more off 
local row=1
foreach row of numlist 1/500{

tempvar order
generate long `order' = _n


capture drop onedraw_partyA_rile
gen onedraw_partyA_rile=rnormal(rile_partyA1,rile_partyA_sd1) /* draw one rile score for party A from the distribution */
bysort rile_partyA1: replace onedraw_partyA_rile = onedraw_partyA_rile[1] /* make sure that the draw is not for 1 observation, but constant within dyad */

capture drop onedraw_partyB_rile
gen onedraw_partyB_rile=rnormal(rile_partyB1,rile_partyB_sd1) /* draw one rile score for party B from the distribution */
bysort rile_partyB1: replace onedraw_partyB_rile = onedraw_partyB_rile[1] /* make sure that the draw is not for 1 observation, but constant within dyad */
sort `order'


capture drop diff_rile /* calculate the diff in rile among the pair */
gen diff_rile=abs(onedraw_partyA_rile-onedraw_partyB_rile)


capture drop onedraw_coopscore_full
gen onedraw_coopscore_full=rnormal(coopscore_meanfull,coopscore_sdfull) /* draw one media score from the distribution */
bysort dyad: replace onedraw_coopscore_full = onedraw_coopscore_full[1] /* make sure that the draw is not for 1 observation, but constant within dyad */


mixed coop onedraw_coopscore_full /// /* use that one draw of the media score in this regression */
				 diff_rile ///
				 percent_month120_bothCab i.countryyear, ||_all:R.groupdyad || respid:	
				  

				  
replace mediascore_C=_b[onedraw_coopscore_full] in `row' 
replace mediascoreSE_C=_se[onedraw_coopscore_full] in `row' 				  
				  

replace ideological_distance_C=_b[diff_rile] in `row' /* store the coefficient of rile in this variable */
replace ideological_distanceSE_C=_se[diff_rile] in `row' /* store the se of rile in this variable */

replace cab_membership_C=_b[percent_month120_bothCab] in `row' /* store the coefficient of cab participation in this variable */
replace cab_membershipSE_C=_se[percent_month120_bothCab] in `row' /* store the se of cab participation in this variable */


replace constant_C=_b[_cons] in `row' 
replace constantSE_C=_se[_cons] in `row' 

replace dyadRE_C=(exp([lns1_1_1]_b[_cons]))^2 in `row'
 _diparm lns1_1_1, f(exp(@)^2) d(2*exp(@)^2)
replace dyadRE_SE_C=r(se) in `row'

replace respRE_C=(exp([lns2_1_1]_b[_cons]))^2 in `row'
 _diparm lns2_1_1, f(exp(@)^2) d(2*exp(@)^2)
replace respRE_SE_C=r(se) in `row'

replace residRE_C=(exp([lnsig_e]_b[_cons]))^2 in `row'
 _diparm lnsig_e, f(exp(@)^2) d(2*exp(@)^2)
replace residRE_SE_C=r(se) in `row'


local row=`row'+1

}


/* Model D */ 

set more off 
local row=1
foreach row of numlist 1/500{

capture drop onedraw_coopscore_full
gen onedraw_coopscore_full=rnormal(coopscore_meanfull,coopscore_sdfull) /* draw one media score from the distribution */
bysort dyad: replace onedraw_coopscore_full = onedraw_coopscore_full[1] /* make sure that the draw is not for 1 observation, but constant within dyad */

mixed coop onedraw_coopscore_full /// /* use that one draw of the media score in this regression */
				 lrdist ///
				 current_bothCab i.countryyear, ||_all:R.groupdyad || respid:	
				  

				  
replace mediascore_D=_b[onedraw_coopscore_full] in `row' 
replace mediascoreSE_D=_se[onedraw_coopscore_full] in `row' 				  
				  

replace ideological_distance_D=_b[lrdist] in `row' /* store the coefficient of rile in this variable */
replace ideological_distanceSE_D=_se[lrdist] in `row' /* store the se of rile in this variable */

replace cab_membership_D=_b[current_bothCab] in `row' /* store the coefficient of cab participation in this variable */
replace cab_membershipSE_D=_se[current_bothCab] in `row' /* store the se of cab participation in this variable */


replace constant_D=_b[_cons] in `row' 
replace constantSE_D=_se[_cons] in `row' 

replace dyadRE_D=(exp([lns1_1_1]_b[_cons]))^2 in `row'
 _diparm lns1_1_1, f(exp(@)^2) d(2*exp(@)^2)
replace dyadRE_SE_D=r(se) in `row'

replace respRE_D=(exp([lns2_1_1]_b[_cons]))^2 in `row'
 _diparm lns2_1_1, f(exp(@)^2) d(2*exp(@)^2)
replace respRE_SE_D=r(se) in `row'

replace residRE_D=(exp([lnsig_e]_b[_cons]))^2 in `row'
 _diparm lnsig_e, f(exp(@)^2) d(2*exp(@)^2)
replace residRE_SE_D=r(se) in `row'

local row=`row'+1

}



/* Model E */ 

set more off 
local row=1
foreach row of numlist 1/500{

capture drop onedraw_coopscore_full
gen onedraw_coopscore_full=rnormal(coopscore_meanfull,coopscore_sdfull) /* draw one media score from the distribution */
bysort dyad: replace onedraw_coopscore_full = onedraw_coopscore_full[1] /* make sure that the draw is not for 1 observation, but constant within dyad */



mixed coop onedraw_coopscore_full /// /* use that one draw of the media score in this regression */
				 lrdist ///
				 perceived_bothCab i.countryyear, ||_all:R.groupdyad || respid:	
				  

				  
replace mediascore_E=_b[onedraw_coopscore_full] in `row' 
replace mediascoreSE_E=_se[onedraw_coopscore_full] in `row' 				  
				  

replace ideological_distance_E=_b[lrdist] in `row' /* store the coefficient of rile in this variable */
replace ideological_distanceSE_E=_se[lrdist] in `row' /* store the se of rile in this variable */

replace cab_membership_E=_b[perceived_bothCab] in `row' /* store the coefficient of cab participation in this variable */
replace cab_membershipSE_E=_se[perceived_bothCab] in `row' /* store the se of cab participation in this variable */


replace constant_E=_b[_cons] in `row' 
replace constantSE_E=_se[_cons] in `row' 

replace dyadRE_E=(exp([lns1_1_1]_b[_cons]))^2 in `row'
 _diparm lns1_1_1, f(exp(@)^2) d(2*exp(@)^2)
replace dyadRE_SE_E=r(se) in `row'

replace respRE_E=(exp([lns2_1_1]_b[_cons]))^2 in `row'
 _diparm lns2_1_1, f(exp(@)^2) d(2*exp(@)^2)
replace respRE_SE_E=r(se) in `row'

replace residRE_E=(exp([lnsig_e]_b[_cons]))^2 in `row'
 _diparm lnsig_e, f(exp(@)^2) d(2*exp(@)^2)
replace residRE_SE_E=r(se) in `row'

local row=`row'+1

}


/* Model F */ 

set more off 
local row=1
foreach row of numlist 1/500{

capture drop onedraw_coopscore_full
gen onedraw_coopscore_full=rnormal(coopscore_meanfull,coopscore_sdfull) /* draw one media score from the distribution */
bysort dyad: replace onedraw_coopscore_full = onedraw_coopscore_full[1] /* make sure that the draw is not for 1 observation, but constant within dyad */

mixed coop onedraw_coopscore_full /// /* use that one draw of the media score in this regression */
				 lrdist ///
				 percent_month120_bothCab i.countryyear, ||_all:R.groupdyad || respid:	
				  

				  
replace mediascore_F=_b[onedraw_coopscore_full] in `row' 
replace mediascoreSE_F=_se[onedraw_coopscore_full] in `row' 				  
				  

replace ideological_distance_F=_b[lrdist] in `row' /* store the coefficient of rile in this variable */
replace ideological_distanceSE_F=_se[lrdist] in `row' /* store the se of rile in this variable */

replace cab_membership_F=_b[percent_month120_bothCab] in `row' /* store the coefficient of cab participation in this variable */
replace cab_membershipSE_F=_se[percent_month120_bothCab] in `row' /* store the se of cab participation in this variable */


replace constant_F=_b[_cons] in `row' 
replace constantSE_F=_se[_cons] in `row' 

replace dyadRE_F=(exp([lns1_1_1]_b[_cons]))^2 in `row'
 _diparm lns1_1_1, f(exp(@)^2) d(2*exp(@)^2)
replace dyadRE_SE_F=r(se) in `row'

replace respRE_F=(exp([lns2_1_1]_b[_cons]))^2 in `row'
 _diparm lns2_1_1, f(exp(@)^2) d(2*exp(@)^2)
replace respRE_SE_F=r(se) in `row'

replace residRE_F=(exp([lnsig_e]_b[_cons]))^2 in `row'
 _diparm lnsig_e, f(exp(@)^2) d(2*exp(@)^2)
replace residRE_SE_F=r(se) in `row'

local row=`row'+1


}

keep mediascore_A - residRE_SE_F




set more off
foreach model in A B C D E F {

rename dyadRE_SE_`model' dyadRESE_`model'
rename respRE_SE_`model' respRESE_`model'
rename residRE_SE_`model' residRESE_`model'



set more off
foreach i of numlist 1/500{


foreach variable in mediascore ideological_distance cab_membership ///
 constant dyadRE respRE residRE{
 
capture drop `variable'`model'_simcoef

gen `variable'`model'_simcoef=rnormal(`variable'_`model',`variable'SE_`model')

}

preserve
keep *_simcoef
tempfile tempscore`model'_`i'
	save `tempscore`model'_`i''
	
restore 
}
}

foreach model in A B C D E F{
use `tempscore`model'_1', clear

foreach i of numlist 2/500{

append using `tempscore`model'_`i''

}
}

/* The coefficients of the variables in all the models are the average of 
the simulated coefficients while
the bootstrapped standard errors are the standard deviation */


sum *A_simcoef /* Model A */
sum *B_simcoef /* Model B */
sum *C_simcoef /* Model C */
sum *D_simcoef /* Model D */
sum *E_simcoef /* Model E */
sum *F_simcoef /* Model F */




